<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>
<script type="text/javascript">
$(function(){
	$.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
	$( "#del-button").button().click( function () {
		if (confirm( "你真的要删除选中的订单吗？" )) {
			idList = "";
			sp = "";
			$( ".checkbox-del" ).each(function(){
				if ($(this).attr("checked") == "checked") {
					idList += sp + $(this).val();
					if (sp == "") sp = ",";
				}
			});
			$.ajax({
				type: "POST",
				url: "/order/del",
				dataType: "json",
				data:"idList="+idList,
				success: function(data) {
					var result=eval(data);
					if (result == true) {
						$( "#queryButton").click();
						alert( "删除成功！" );
					} else {
						alert( "删除失败！" );
					}
				}
			});
		}
	});
    $( "#queryButton").button().click( function () {
		$("#starttime2").val($("#starttime").val());$("#endtime2").val($("#endtime").val());
		$("#commodity2").val($("#commodity").val());$("#member2").val($("#member").val());
		$("#state2").val($("#state").val());
	    $.ajax({
			type: "POST",
			url: "/order/query",
			dataType: "json",
			data:"starttime="+$("#starttime").val()+"&endtime="+$("#endtime").val()+"&commodity="+$("#commodity").val()+"&member="+$("#member").val()+"&state="+$("#state").val()+"&pagesize="+pager.size,
			success: function(data) {
				var items=eval(data.items);
				bindDataTable(items)
				pager.itemCount = data.count;
				pager.index = 1
				pager.render();
			}
		});

		return false; 
	});
	$( "#starttime" ).datepicker();
	$( "#endtime" ).datepicker();
	var pager = new PagerView('pager');
	pager.size = 15; // 每页显示15条记录
	pager.maxButtons = 10;
	pager.itemCount = 0;
	pager.onclick = function(index) {
		$.ajax({
			type: "POST",
			url: "/order/pager",
			dataType: "json",
			data:"starttime="+$("#starttime2").val()+"&endtime="+$("#endtime2").val()+"&commodity="+$("#commodity2").val()+"&member="+$("#member2").val()+"&state="+$("#state2").val()+"&pagesize="+pager.size+"&pageindex="+index,
			success: function(data) {
				var items=eval(data);
				bindDataTable(items)
			}
		});
		
	};
	pager.render();

	$( "#queryButton").click();
});
function bindDataTable(items) {
	$(".datatable tbody").empty();
	$.each(items, function(k)
	{
		$(".datatable tbody").append("<tr><td><input type=\"checkbox\" value=\""+items[k]['id']+"\" class=\"checkbox-del\" /></td><td>"+items[k]['id']+"</td><td>"+items[k]['userId']+"</td><td>"+items[k]['productTotal']+"</td><td>"+items[k]['parentId']+"</td><td>"+items[k]['shipFee']+"</td><td>"+items[k]['sum']+"</td><td>"+items[k]['shipFee']+"</td><td>"+items[k]['comment']+"</td><td>"+items[k]['dateCreated']+"</td><td>"+items[k]['status']+"</td><td><img src=\"/admins/images/edit.gif\" id=\""+items[k]['id']+"\" class=\"edit-operation\"/></td></tr>");
	});
	$( ".edit-operation").click( function () {
		showDialog(this.id);
	});
	$( ".datatable tbody tr" ).each(function(){
		$(this).click( function () { 
			$( ".datatable .select" ).removeClass("select");
			$(this).addClass("select"); 
		});
		$(this).hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);
	});
}
</script>
<form action="/order" method="post" style="margin:0px">
<div style=" border:1px solid #DDDDDD; padding:2px; margin:2px;">
<label for="starttime">订单时间：</label><input type="text" id="starttime" name="starttime" />
<label for="endtime">--</label><input type="text" id="endtime" name="endtime" />
<label for="commodity">商品：</label><input type="text" id="commodity" name="commodity" />
<label for="member">用户：</label><input type="text" id="member" name="member" />
<label for="state">状态：</label>
<select id="state" name="state">
  <option value="">全部</option>
  <option value="0">待处理</option>
  <option value="1">已处理</option>
</select>
<input type="submit" id="queryButton" value="查询"/>
</div>
</form>
<style>
	.datatable {
		text-align: left;
		font-size: 12px;
		font-family: verdana;
		background: #c0c0c0; margin:2px;
	}

	.datatable thead tr,

	.datatable tfoot tr {
		background: #c0c0c0;
	}

	.datatable tbody tr {
		background: #f0f0f0;
	}

	.datatable td, th {
		border: 1px solid white; height:28px;
	}
	.datatable td {background-color:white;}
	
	.datatable .hover td {background-color:#99CCFF;}
	
	.datatable .select td {background-color:#EEEE99;}

	#ui-datepicker-div{ font-size:12px;}
</style>
<div style="padding:2px; margin:2px;">
<table cellspacing="1" width="100%" class="datatable">
	<thead>
		<tr>
            <th></th>
			<th>ID</th>
			<th>用户</th>
			<th>商品名称</th>
			<th>来源</th>
			<th>单价</th>
			<th>数量</th>
			<th>邮费</th>
            <th>快递单号</th>
            <th>提交时间</th>
            <th>状态</th>
            <th>操作</th>
		</tr>
	</thead>
    <tfoot>
      <tr><td colspan="12"><button id="del-button">删除选中</button></td></tr>
    </tfoot>
	<tbody>
	</tbody>
</table>
</div>
<form action="/order/pager" method="post">
<input type="hidden" id="starttime2" name="starttime2" value="" />
<input type="hidden" id="endtime2" name="endtime2" value="" />
<input type="hidden" id="commodity2" name="commodity2" value="" />
<input type="hidden" id="member2" name="member2" value="" />
<input type="hidden" id="state2" name="state2" value="" />
</form>
<div id="pager" class="pager"></div>
<style type="text/css">
#dialog-modal { display:none; }
#dialog-modal input { width:170px;}
</style>
<script type="text/javascript">
$(function(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#dialog-modal" ).dialog({
		height: 360,
		width: 600,
		modal: true,
		autoOpen: false
	});
    $( "#edit-button-ok").button().click( function () {
		orderId = $("#order-id").val();orderName = $("#order-name").val();orderPrice = $("#order-price").val();
		orderSum = $("#order-sum").val();orderPostage = $("#order-postage").val();orderDate = $("#order-date").val();
		orderState = $("#order-state").val();expressDelivery = $("#express-delivery").val();
		productComment = $("#product-comment").val();orderComment = $("#order-comment").val();
		$.ajax({
			type: "POST",
			url: "/order/edit",
			dataType: "json",
			data:"orderId="+$("#order-id").val()+"&orderName="+orderName+"&orderPrice="+orderPrice+"&orderSum="+orderSum+"&orderPostage="+orderPostage+"&orderDate="+orderDate+"&orderState="+orderState+"&expressDelivery="+expressDelivery+"&productComment="+productComment+"&orderComment="+orderComment,
			success: function(data) {
				var result=eval(data);
				if (result == true) {
					alert( "更新成功！" );
					row = $( ".datatable .select" ).children();
					$(row).eq(10).text(orderState);
					$( "#dialog-modal" ).dialog( "close" );
				} else {
					alert( "更新失败！" );
				}
			}
		});
	});
	$( "#edit-button-cancel").button().click( function () {
		$( "#dialog-modal" ).dialog( "close" );
	});
});
function showDialog(orderId) {
	$.ajax({
		type: "POST",
		url: "/order/details",
		dataType: "json",
		data:"orderId="+orderId,
		success: function(data) {
			var order=eval(data);
			if (order != false) {
			    $("#order-id").val( order.id );
				$("#order-name").val( order.orderNo );
				$("#order-price").val( order.shipFee );
				$("#order-sum").val( order.sum );
				$("#order-postage").val( order.shipFee );
				$("#order-date").val( order.dateCreated );
				$("#order-state").val( order.status );
				$("#express-delivery").val( order.productTotal );
				$("#product-comment").val( order.comment );
				$("#order-comment").val( order.comment );
				$( "#dialog-modal" ).dialog( "open" );
			} else {
				alert( "获取数据失败！" );
			}
		}
	});
}
</script>
<div id="dialog-modal" title="订单操作">
	<table width="100%">
      <tr>
        <td align="right" width="100">名称：</td><td colspan="3"><input id="order-name" type="text" value="" style="width:99%;" /></td>
      </tr>
      <tr>        
        <td align="right">单价：</td><td><input id="order-price" type="text" value="" /></td><td width="100" align="right">数量：</td><td><input id="order-sum" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">邮费：</td><td><input id="order-postage" type="text" value="" /></td><td align="right">提交时间：</td><td><input id="order-date" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">状态：</td><td>
        <select id="order-state">
          <option value="">全部</option>
          <option value="0">待处理</option>
          <option value="1">已处理</option>
        </select></td><td align="right">快递单号：</td><td><input id="express-delivery" type="text" value="" /></td>
     </tr>
     <tr>
        <td align="right">商品备注：</td><td colspan="3"><textarea id="product-comment" rows="3" style="width:99%"></textarea></td>
     </tr>
     <tr>
        <td align="right">订单备注：</td><td colspan="3"><textarea id="order-comment" rows="3" style="width:99%"></textarea></td>
     </tr>
     <tr>
       <td colspan="4" align="center">
       <button id="edit-button-ok">确认更新</button>
       <button id="edit-button-cancel">取消返回</button><input type="hidden" id="order-id" value="" />
       </td>
     </tr>
    </table>
</div>